<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/10/22 0022
 * Time: 上午 1:46
 */
namespace Admin\Controller;
use Org\Util\Page;
class CustomsController extends CommonController
{
    public function _initialize()
    {
        parent::_initialize();
        $this->_mod = D('Customs');
    }
    public function index (){

        $map = $this->_search();
        $mod = D($this->_name);
        !empty($mod) && $this->_list($mod, $map);
        $this->options = $this->check();
        $this->display();
    }
    protected function _list($model, $map = array(), $pagesize=20){
        //角色信息
        $info = D ('Admin')->get_role ($_SESSION['id']);
        //如果管理员没去完善自己的区域
        if (empty ($info['scope'])) $this->error('请去完善个人信息','admin.php/index/index',5);
        //查找管理员下面的人员ID
        $users = classify ($info['level'], $info['scope']);
        $map['user_id'] = array ('in', $users);
        $count = $model->where($map)->count('id');
        $pager = new Page($count, $pagesize);
        $page = $pager->show();
        $this->assign("page", $page);

        $list = $model->get_customs ($map, $pager->firstRow, $pager->listRows);
        $this->assign('list', $list);//var_dump($list);exit;
        $this->assign('list_table', true);
    }
    protected function _search() {
        $map = array();
        ($time_start = I('get.time_start')) && $map['add_time'][] = array('egt', strtotime($time_start));
        ($time_end = I('get.time_end')) && $map['add_time'][] = array('elt', strtotime($time_end)+(24*60*60-1));
        ($keyword = I('get.keyword')) && $map['title'] = array('like', '%'.$keyword.'%');
        $area_id = I('get.area_id',0,'int');
        if( $_GET['status']==null ){
            $status = -1;
        }else{
            $status = intval($_GET['status']);
        }
        $status>=0 && $map['status'] = array('eq',$status);
        $selected_ids = '';
        if ($area_id) {
            $id_arr = $this->_cate_mod->get_child_ids($area_id, true);
            $map['area_id'] = array('IN', $id_arr);
            $spid = $this->_cate_mod->where(array('id'=>$area_id))->getField('spid');
            $selected_ids = $spid ? $spid . $area_id : $area_id;
        }

        $this->assign('search', array(
            'time_start' => $time_start,
            'time_end' => $time_end,
            'area_id' => $area_id,
            'selected_ids' => $selected_ids,
            'status'  => $status,
            'keyword' => $keyword
        ));
        return $map;
    }
    public function _before_index() {
        $p = I('p',1,'intval');
        $this->list_relation = true;
        $this->assign('p',$p);
    }
}